package com.iplant.aps.service.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.iplant.aps.service.po.OutResult;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.serviceimpl.dao.BaseDAO;
import com.iplant.aps.serviceimpl.dao.mes.MESSystemConfigDAO;

/**
 * 调度任务配置类
 * 
 * @author YouWang·Peng
 *  2025-1-16 11:06:45
 */
@Configuration
@EnableScheduling
public class ScheduledConfig  {

	private static final Logger logger = LoggerFactory.getLogger(ScheduledConfig.class);
	
	public String getCronExpression(String key) {
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            // 从数据库配置获取cron表达式
            String cronExpression = MESSystemConfigDAO.getInstance().SelectValueByKey(BaseDAO.SysAdmin, key, wErrorCode);
            // 如果未配置,使用默认值
            if (StringUtils.isEmpty(cronExpression)) {
                return "0 0 1 * * ?";  // 默认每天凌晨1点执行
            }
            return cronExpression;
        } catch (Exception e) {
            logger.error(e.toString());
            return "0 0 1 * * ?";
        }
    }
}
